package threadings;

/**
 * Created with IntelliJ IDEA.
 * Description:  懒汉模式
 * User: yyx
 * Date: 2024-02-07
 * Time: 13:48
 */
class SingletonLazy{
    volatile private static SingletonLazy singleton = null;
    private SingletonLazy(){};//构造方法

    public static SingletonLazy getSingleton() {
        if(singleton == null) {
            synchronized (SingletonLazy.class) {
                if (singleton == null) {
                    singleton = new SingletonLazy();

                }
            }
        }
        return singleton;
    }
}
public class ThreadDemo13 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getSingleton();
        SingletonLazy s2 = SingletonLazy.getSingleton();
        System.out.println(s1==s2);
        //不能执行新的new操作
//        SingletonLazy s3 = new SingletonLazy();

    }

}
